package inet.app.controller.parser;


import inet.app.controller.connect.JsonParser;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.util.SmartLog;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

public class Parser_Kqxs {

	private ArrayList<KqGiai_Item> list = new ArrayList<KqGiai_Item>();
	private KqGiai_Item item;
	private String ITEMS = "items";
	private String ITEM ="item";
	private String NAME = "name";
	private String CODE = "code";
	private String DATE = "date";
	private String DB = "DB";
	private String G1 = "G1";
	private String G2 = "G2";
	private String G3 = "G3";
	private String G4 = "G4";
	private String G5 = "G5";
	private String G6 = "G6";
	private String G7 = "G7";
	private String G8 = "G8";
	
	public Parser_Kqxs () {
		
	}
	public ArrayList<KqGiai_Item> getList(String url) {
		list = new ArrayList<KqGiai_Item>();
		JSONObject jobject,object,job_items;
		JsonParser jParser = new JsonParser();
		jobject = jParser.getJSONFromUrl(url);
		try {
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String date= job_items.getString(DATE);
				JSONArray ja = job_items.getJSONArray(ITEM);
				int i = 0;
				String code,name,giaidb,giai1,giai2,giai3,giai4,giai5,giai6,giai7,giai8;
				
				for (i = 0; i < ja.length(); i++) {
					object = ja.getJSONObject(i);
					code = object.getString(CODE);
					name= object.getString(NAME);
					giaidb = object.getString(DB);
					giai1 = object.getString(G1);
					giai2 = object.getString(G2);
					giai3 = object.getString(G3);
					giai4 = object.getString(G4);
					giai5 = object.getString(G5);
					giai6 = object.getString(G6);
					giai7 = object.getString(G7);
					giai8 = object.getString(G8);
					item = new KqGiai_Item(code, name, date, giaidb, giai1, giai2, giai3, giai4, giai5, giai6, giai7, giai8);
					list.add(item);
				}
			return list;
        } catch (Exception e) {
            SmartLog.e("Error parsing data " + e.toString());
            return null;
        }
		
	}
	public ArrayList<KqGiai_Item> getListMB(String url) {
		list = new ArrayList<KqGiai_Item>();
		JSONObject jobject,object,job_items;
		
		try {
			jobject = new JSONObject(url);
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String date= job_items.getString(DATE);
				JSONArray ja = job_items.getJSONArray(ITEM);
				int i = 0;
				String code,name,giaidb,giai1,giai2,giai3,giai4,giai5,giai6,giai7,giai8;
				
				for (i = 0; i < ja.length(); i++) {
					object = ja.getJSONObject(i);
					code = object.getString(CODE);
					name= object.getString(NAME);
					giaidb = object.getString(DB);
					giai1 = object.getString(G1);
					giai2 = object.getString(G2);
					giai3 = object.getString(G3);
					giai4 = object.getString(G4);
					giai5 = object.getString(G5);
					giai6 = object.getString(G6);
					giai7 = object.getString(G7);
					giai8 = object.getString(G8);
					item = new KqGiai_Item(code, name, date, giaidb, giai1, giai2, giai3, giai4, giai5, giai6, giai7, giai8);
					list.add(item);
				}
			return list;
        } catch (Exception e) {
            SmartLog.e("Error parsing data " + e.toString());
            return null;
        }
		
	}
	
	public ArrayList<KqGiai_Item> getList_Mt(String url) {
		list = new ArrayList<KqGiai_Item>();
		JSONObject jobject,object,job_items;
		JsonParser jParser = new JsonParser();
		jobject = jParser.getJSONFromUrl(url);
		try {
				String items = jobject.getString(ITEMS);
				System.out.println("ITEMS :" +items);
				job_items = new JSONObject(items);
				String name= job_items.getString(NAME);
				String code= job_items.getString(CODE);
				JSONArray ja = job_items.getJSONArray(ITEM);
				int i = 0;
				String date,giaidb,giai1,giai2,giai3,giai4,giai5,giai6,giai7,giai8;
				
				for (i = 0; i < ja.length(); i++) {
					object = ja.getJSONObject(i);
					date = object.getString(DATE);
					giaidb = object.getString(DB);
					giai1 = object.getString(G1);
					giai2 = object.getString(G2);
					giai3 = object.getString(G3);
					giai4 = object.getString(G4);
					giai5 = object.getString(G5);
					giai6 = object.getString(G6);
					giai7 = object.getString(G7);
					giai8 = object.getString(G8);
					item = new KqGiai_Item(code, name, date, giaidb, giai1, giai2, giai3, giai4, giai5, giai6, giai7, giai8);
					list.add(item);
				}
			return list;
        } catch (Exception e) {
            SmartLog.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}
	
	
}
